package com.bookmall.product.web;

import com.alibaba.fastjson.TypeReference;
import com.bookmall.common.constant.DefaultDataConstants;
import com.bookmall.product.domain.BookCategory;
import com.bookmall.product.domain.BookInfo;
import com.bookmall.product.domain.BookInfoImages;
import com.bookmall.product.domain.vo.BookInfoVo;
import com.bookmall.product.domain.vo.CartInfoVo;
import com.bookmall.product.domain.vo.CartItemVo;
import com.bookmall.product.feign.OrderFeignService;
import com.bookmall.product.service.IBookCategoryService;
import com.bookmall.product.service.IBookInfoService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import sun.nio.ch.ThreadPool;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-03-17-17:12
 */
@Controller
public class IndexController {
    @Autowired
    private IBookCategoryService bookCategoryService;

    @Autowired
    private IBookInfoService bookInfoService;

    @Autowired
    private OrderFeignService orderFeignService;

    @Autowired
    private ThreadPoolExecutor executor;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model, HttpSession session) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> categories = CompletableFuture.runAsync(() -> {
            //查出所有的分类菜单
            List<BookCategory> categoryList = bookCategoryService.listWithTreeForStatus();
            model.addAttribute("categories", categoryList);
        }, executor);

        CompletableFuture<Void> latestBooks = CompletableFuture.runAsync(() -> {
            //查出最新发布书籍
            List<BookInfoVo> latestBookList = bookInfoService.selectLatestBookInfoList();
            model.addAttribute("latestBooks", latestBookList);
        }, executor);

        CompletableFuture<Void> specialBooks = CompletableFuture.runAsync(() -> {
            //查出特价书籍
            List<BookInfoVo> specialBookList = bookInfoService.selectSpecialPriceBookList();
            model.addAttribute("specialBooks", specialBookList);
        }, executor);

        CompletableFuture<Void> minPriceBooks = CompletableFuture.runAsync(() -> {
            //查出最便宜书籍
            List<BookInfoVo> minPriceBookList = bookInfoService.selectMinPriceBook();
            model.addAttribute("minPriceBooks", minPriceBookList);
        }, executor);

        CompletableFuture<Void> levelBooks = CompletableFuture.runAsync(() -> {
            //查出书籍新旧程度最高的书籍
            List<BookInfoVo> maxLevelBookList = bookInfoService.selectMaxLevelBook();
            model.addAttribute("maxLevelBooks", maxLevelBookList);
        }, executor);

        CompletableFuture<Void> cartBooks = CompletableFuture.runAsync(() -> {
            //查出书籍新旧程度最高的书籍
            AjaxResult ajaxResult = orderFeignService.getCartInfoByLoginMember();
            CartInfoVo cartInfoVo = new CartInfoVo();
            if(ajaxResult.get(AjaxResult.CODE_TAG).equals(200)){
                cartInfoVo = ajaxResult.getData("data", new TypeReference<CartInfoVo>() {});
            } else {
                cartInfoVo.setItems(new ArrayList<>());
                cartInfoVo.setFailReason("您还没有登录，请登录后查看");
            }
            model.addAttribute("cartInfo", cartInfoVo);
        }, executor);

        CompletableFuture.allOf(categories, latestBooks, specialBooks, minPriceBooks, levelBooks, cartBooks).get();

        System.out.println(session.getAttribute(DefaultDataConstants.LOGIN_USER));
        System.out.println(session.getId());

        return "index";
    }


}
